package graphic;

import input.Input;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import klient.Klient;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;

/**
 *  
 * @author Marcin & Bartek
 * 
 * @description Transforms screen pointer to the terrain pointer
 */
public final class PointerTransform {
    private PointerTransform(){}
    private static IntBuffer view=Buffers.intBuffer(new int[16]);
    private static FloatBuffer model=Buffers.floatBuffer(new float[16]);
    private static FloatBuffer proj=Buffers.floatBuffer(new float[16]);
    private static FloatBuffer outz=Buffers.floatBuffer(new float[1]);
    public static FloatBuffer out=Buffers.floatBuffer(new float[3]);;
    
    private static boolean init=false;
    
    private static float ox=Input.x;
    private static float oy=Input.y;
    public static void reset(){
        init=false;
    }
    
    public static void refresh(){
        if(!init){
            GL11.glGetInteger(GL11.GL_VIEWPORT, view);
            GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, proj);  
            init = true;
        } 
        GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, model);
             
        
        GL11.glReadPixels((int)ox, (int)oy, 1, 1, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, outz);
        ox=Input.x;
        oy=Input.y;
        GLU.gluUnProject(ox, oy, outz.get(0), model, proj, view, out);
    }
}
